package com.jcodecraeer.xrecyclerview.progressindicator.indicator;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.RectFloat;

import java.util.ArrayList;
import java.util.List;

public class BallClipRotatePulseIndicator extends BaseIndicatorController {

    private float scaleFloat1;
    private float scaleFloat2;
    private float degrees;
    @Override
    public void draw(Canvas canvas, Paint paint) {
        float circleSpacing=12;
        float x=getWidth()/2;
        float y=getHeight()/2;

        //draw fill circle
        canvas.save();
        canvas.translate(x, y);
        canvas.scale(scaleFloat1, scaleFloat1);
        paint.setStyle(Paint.Style.FILL_STYLE);
        canvas.drawCircle(0, 0, x / 2.5f, paint);

        canvas.restore();

        canvas.translate(x, y);
        canvas.scale(scaleFloat2, scaleFloat2);
//        canvas.rotate(degrees*360,x,y);

        paint.setStrokeWidth(3);
        paint.setStyle(Paint.Style.STROKE_STYLE);

        //draw two arc
        float[] startAngles=new float[]{225,45};
        for (int i = 0; i < 2; i++) {
            RectFloat rectF=new RectFloat(-x+circleSpacing,-y+circleSpacing,x-circleSpacing,y-circleSpacing);
            Arc arc = new Arc();
            arc.setArc(startAngles[i], 90, false);
            canvas.drawArc(rectF,arc , paint);
        }
    }

    @Override
    public List<Animator> createAnimation() {
        AnimatorValue scaleAnim = new AnimatorValue();
        scaleAnim.setDuration(1000);
        scaleAnim.setLoopedCount(-1);
        scaleAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                scaleFloat1 = v;
                postInvalidate();
            }
        });
        scaleAnim.start();

        AnimatorValue scaleAnim2 = new AnimatorValue();
        scaleAnim2.setDuration(1000);
        scaleAnim2.setLoopedCount(-1);
        scaleAnim2.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                scaleFloat2 = v;
                getTarget().setRotation(v*360);
                postInvalidate();
            }
        });
        scaleAnim2.start();

        AnimatorValue rotateAnim = new AnimatorValue();
        rotateAnim.setDuration(1000);
        rotateAnim.setLoopedCount(-1);
        rotateAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                degrees = v;
                postInvalidate();
            }
        });
        rotateAnim.start();

        List<Animator> animators=new ArrayList<>();
        animators.add(scaleAnim);
        animators.add(scaleAnim2);
        animators.add(rotateAnim);
        return animators;
    }
}
